var app = getApp();
var wxpay = require('../../utils/pay.js')
// 语言
var util = require('../../utils/util.js')

Page({
    data:{
      orderDetail:{}
    },
    gotodetail(){
      console.log(this.data.orderDetail)
      // wx.navigateTo({
      //   url: '/pages/goods-detail/goods-detail?id='+this.data.orderDetail.order_id,
      // })
    },
    previewImg(e){
      var index = e.currentTarget.dataset.index;
      var imgArr = this.data.orderDetail.pic;
      wx.previewImage({
        current: imgArr[index],     //当前图片地址
        urls: imgArr,               //所有要预览的图片的地址集合 数组形式
      })
    },
    onLoad:function(e){
      var that = this;
      if(!e.share){ 
        that.setData({ share: true }); 
      }
      that.setData(e); 
      if (app.globalData.iphone == true) { that.setData({ iphone: 'iphone' }) }
      if(e.b==1){
        this.getBackOrderDetail(e.id)
      }else{
        this.getOrderDetail(e.id,e.other)
      }
    },
    // 查看售后详情
    // showBackDetail(){
    //   let item = this.data.orderDetail;
    //   console.log('-----item',item)
    //   let status = item.order_status, params = "", type = item.application_type=="退货" ? 2:1;
    //   console.log(status);
    //   switch (status){
    //     case "after_sale_waiting_return": // 等待审核
    //       params = "isSuccess=true&type=" + type
    //       break;
    //     case "after_sale_return_accept": // 已通过审核（填写联系人）
    //       params = "type=3"
    //       break;
    //     case "after_sale_return_dispatch": // 已发货状态
    //       params = "isSuccess=true&status=已发货状态"
    //       break;
    //     case "after_sale_return_received": // 已收货
    //       params = "isSuccess=true&status=已收货"
    //       break;
    //     case "after_sale_return_refund": // 完成
    //       params = "isSuccess=true&isComplete=true"
    //       break;
    //   }
    //   console.log("参数",params)
    //   // wx.navigateTo({
    //   //   url: "/pages/apply-for-return/apply-for-return?id=" + item.id + "&" + params
    //   // })
    // },
    // 获取售后订单详情
    getBackOrderDetail(id){
      var that=this;
      wx.request({
        url: app.globalData.urls + '/order/process-detail',
        method: "POST",
        header: app.getPostRequestHeader(),
        data: {
          id,
        },
        success(res) {
          let val=res.data.data
          switch(val.status){
            case "after_sale_waiting_return":
              val.status_text = "申请中"
            break;
            case "after_sale_return_accept":
              val.status_text = "审核通过"
            break;
            case "after_sale_return_refuse":
              val.status_text = "申请拒绝"
            break;
            case "after_sale_return_cancel":
              val.status_text = "取消"
            break;
            case "after_sale_return_dispatch":
              val.status_text = "已发货"
            break;
            case "after_sale_return_received":
              val.status_text = "已收货"
            break;
            case "after_sale_return_refund":
              val.status_text = "已退款"
            break;
            case "after_sale_return_admin_dispatch":
              val.status_text = "商家已发货"
            break;
            case "after_sale_return_admin_received":
              val.status_text = "商家已收货"
            break;
        }
          that.setData({
            orderDetail:val
          });
          console.log("售后数据",val)
        }
      })
    },
    // 获取订单详情
    getOrderDetail(order_id,other){
      var that = this;
      wx.request({
        url: app.globalData.urls + '/order-manage/detail',
        header: app.getRequestHeader(),
        method: 'POST',
        data: {
          order_id
        },
        success: function (res) {
          let data  = res.data.data
          switch(data.order_status){
              case "payment_pending":
                data.status = "待支付"
              break;
              case "payment_confirmed":
                data.status = "待发货"
              break;
              case "dispatched":
                data.status = "已发货"
              break;
              case "completed":
                data.status = "已完成"
              break;
              case "order_received":
                data.status = "已收货"
              break;
              case "payment_canceled":
                data.status = "已取消"
              break;
              case "holded":
                data.status = "待审核"
              break;
              case "cancle":
                data.status = "已退款已取消"
              break;
              case "待成团":
                data.status = "待成团"
              break;
          }
          if(other){
            data = Object.assign(data,JSON.parse(other))
          }
          that.setData({
            orderDetail:data
          });
        }
      })
    },
    onShow : function () {
    },
     // 同意退款
  confrimReturn(e){
    var that = this;
    wx.showModal({
      title:"是否确定退款",
      success:function(){
        wx.request({
          url: app.globalData.urls + '/order-manage/pay',
          header: app.getRequestHeader(),
          method: 'POST',
          data: {
            order_id:this.data.orderDetail.order_id,
          },
          success: function (res) {
            console.log('---退款---',res.data.data)
            if(res.data.code==200){
              wx.showToast({
                title: '退款成功',
                icon:'none'
              })
            }else{
              wx.showToast({
                title: res.data.message,
                icon:'none'
              })
            }
          }
        })
      }
    })
  },
    // 取消订单
  deleteOrder(e){
    let item = this.data.orderDetail;
    var that = this;
    wx.showModal({
      title:"是否取消订单",
      success:function(){
        wx.request({
          url: app.globalData.urls + '/order-manage/cancel',
          header: app.getRequestHeader(),
          method: 'POST',
          data: {
            order_id:item.order_id
          },
          success: function (res) {
            console.log('---取消订单---',res.data.data)
            if(res.data.code==200){
              wx.showToast({
                title: '删除成功',
                icon:'none'
              })
            }else{
              wx.showToast({
                title: '取消失败',
                icon:'none'
              })
            }
          }
        })
      },
    })
  },
  // 发货
  sendGoods(){
    wx.navigateTo({
      url: "/pages/apply-for-return/apply-for-return?type=3&title=发货&info=" + JSON.stringify(this.data.orderDetail)
    })
  },
  // 退款
  returnOrder(){
    wx.navigateTo({
      url: "/pages/apply-for-return/apply-for-return?type=4&title=退款申请&info=" + JSON.stringify(this.data.orderDetail)
    })
  },
  // 商家--- 
  // 取消订单取消拼团
  cancelOrderTap: function (e) {
    var item=this.data.orderDetail;
    var orderId = item.increment_id, statusstr = e.currentTarget.dataset.statusstr;
    wx.showModal({
      title: statusstr == "待收货" && "待收货订单取消，需客服审核，是否取消订单？" || "是否取消订单", //'确定要取消该订单吗？',  // Are you sure you want to cancel the order?
      content: '',
      success: function (res) {
        if (res.confirm) {
          wx.showLoading();
          wx.request({
            url: app.globalData.urls + '/customer/order/cancel',
            data: {
              bdmin_user_id:util.share_bdmin_user_id(),
              orderId: orderId
            },
            header: app.getRequestHeader(),
            success: (res) => {
              wx.hideLoading();
              if (res.data.code == 200) {
                wx.showToast({
                  title: '成功取消订单',
                })
              }
              app.saveReponseHeader(res);
            }
          })
        }
      }
    })
  },
  toPayTap: function (e) {
    var item=this.data.orderDetail;
    var orderId = item.increment_id;
    var money = item.subtotal, groupid = item.group_id;
    wxpay.wxpay(app, money, orderId, "/pages/order-list/order-list?currentType=1&share=1&groupid=" + groupid);
  },
   // 获取分享图
  getShareImg(price,show_price,product_id,img_url){
    var that = this;
   return new Promise(resolve=>{
    wx.request({
      url: app.globalData.urls + '/product/share-image',
      method: 'POST',
      data: {
        bdmin_user_id:util.share_bdmin_user_id(),
        price,
        show_price,
        product_id,
        img_url
      },
      header: app.getRequestHeader(),
      success (res) {
        resolve(res.data.data.share_img)
      }
    })
   })
  },
  // 查看物流
  goLog(e){
    let getItem = this.data.orderDetail,type=e.currentTarget.dataset.type=="s";
    wx.navigateTo({
      url: "/pages/logistics/logistics?id=" + getItem.increment_id + "&num=" + (getItem.qty||getItem.product.qty) + "&pic=" + (getItem.image||getItem.product.image) + "&name=" + (getItem.name||getItem.product.product_name)+"&company_code="+(type && getItem.company_code2 || getItem.company_code)+"&tracking_number="+(type && getItem.tracking_number2 || getItem.tracking_number)+"&tracking_company="+(type && getItem.tracking_company2 || getItem.tracking_company)
    })
  },
  // 确认收货
  toConfirmTap:function(e){
	  let order_id = this.data.orderDetail;
	  wx.showModal({
      title: "确认您已收到商品？", 
      content: '',
      success: function(res) {
        if (res.confirm) {
          wx.showLoading();
          wx.request({
            url: app.globalData.urls + '/customer/order/delivery',
            data: {
              bdmin_user_id:util.share_bdmin_user_id(),
              orderId: order_id
            },
            header: app.getRequestHeader(),
            success: (res) => {
              console.log("确认收货",res)
              if (res.data.code == 200) {
                wx.showToast({
                  title: '确认收货成功',
                  icon: 'success',
                  duration: 1000
                })
              }
              wx.hideLoading();
            }
          })
        }
      }
	  })
  },
  // 再次购买
  againBuy(e){
    let dataset = this.data.orderDetail;
    // 订单支付页面
    wx.navigateTo({
      url: "/pages/pay-order/pay-order?orderType=cart&groupid=" + dataset.group_id + "&product_info=" + JSON.stringify([{ product_id: dataset.product_id, qty: 1}]) + "&buy_type=1"
    })
  },
  // 去评价
  toEvalTap(e){
    let getItem =this.data.orderDetail;
    wx.navigateTo({
      url: "/pages/eval-bask/eval-bask?pic=" + getItem.product.image + "&name=" + getItem.product.product_name + '&id=' + getItem.item_id
    })
  },
/**
   * 用户点击右上角分享
   */
  onShareAppMessage:async function(ops) {
    let params = this.data.orderDetail;
    console.log("参数",params)
    var userInfo = wx.getStorageSync('userInfo')
    var  share_img= await this.getShareImg(params.product.price,params.show_price,Number(params.product_id),params.product.image);
    console.log(share_img,'----------')
    return {
      // title: '天天精选',
      title: '我是' + userInfo.nickName+'，我在天天精选挑选这些好物，团长快来接单',
      desc: '我是' + userInfo.nickName+'，我在天天精选挑选这些好物，团长快来接单',
      path: '/pages/share/share?order=' + params.order_id + "&id=" + params.product_id + "&groupid=" + params.group_id+"&isShare=1&getBdmin_user_id="+util.share_bdmin_user_id(),
      imageUrl: share_img ,  //用户分享出去的自定义图片大小为5:4,
      success: function (res) {
        // 转发成功
        wx.showToast({
          title: "邀请成功",
          icon: 'success',
          duration: 2000
        })
      },
      fail: function (res) {
        // 分享失败
        wx.showToast({
          title: "邀请失败",
          icon: 'error',
        })
      },
    }

  },

})